package com.woniu.finance.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.woniu.book.domain.Booking;
import com.woniu.finance.feign.BookFeign;
import com.woniu.finance.feign.UserFeign;
import com.woniu.finance.mapper.ApartFeeMapper;
import com.woniu.user.domain.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniu.finance.mapper.OverdueAlarmMapper;
import com.woniu.finance.domain.OverdueAlarm;
import com.woniu.finance.service.IOverdueAlarmService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户欠费报警Service业务层处理
 *
 * @author yy
 * @date 2023-04-04
 */
@Service
public class OverdueAlarmServiceImpl implements IOverdueAlarmService {
    @Autowired
    private OverdueAlarmMapper overdueAlarmMapper;

    @Autowired
    private ApartFeeMapper apartFeeMapper;

    @Autowired
    private BookFeign bookFeign;

    @Autowired
    private UserFeign userFeign;

    /**
     * 查询客户欠费报警
     *
     * @param id 客户欠费报警主键
     * @return 客户欠费报警
     */
    @Override
    public OverdueAlarm selectOverdueAlarmById(Long id) {
        return overdueAlarmMapper.selectOverdueAlarmById(id);
    }

    /**
     * 查询客户欠费报警列表
     *
     * @param overdueAlarm 客户欠费报警
     * @return 客户欠费报警
     */
    @Override
    public List<OverdueAlarm> selectOverdueAlarmList(OverdueAlarm overdueAlarm) {
        return overdueAlarmMapper.selectOverdueAlarmList(overdueAlarm);
    }

    /**
     * 新增客户欠费报警
     *
     * @param overdueAlarm 客户欠费报警
     * @return 结果
     */
    @Override
    public int insertOverdueAlarm(OverdueAlarm overdueAlarm) {
        return overdueAlarmMapper.insertOverdueAlarm(overdueAlarm);
    }

    /**
     * 修改客户欠费报警
     *
     * @param overdueAlarm 客户欠费报警
     * @return 结果
     */
    @Override
    public int updateOverdueAlarm(OverdueAlarm overdueAlarm) {
        return overdueAlarmMapper.updateOverdueAlarm(overdueAlarm);
    }

    /**
     * 批量删除客户欠费报警
     *
     * @param ids 需要删除的客户欠费报警主键
     * @return 结果
     */
    @Override
    public int deleteOverdueAlarmByIds(Long[] ids) {
        return overdueAlarmMapper.deleteOverdueAlarmByIds(ids);
    }

    /**
     * 删除客户欠费报警信息
     *
     * @param id 客户欠费报警主键
     * @return 结果
     */
    @Override
    public int deleteOverdueAlarmById(Long id) {
        return overdueAlarmMapper.deleteOverdueAlarmById(id);
    }

    @Override
    @Transactional
    public void checkRentDeadline() {
        //查出今日需要交房租的订单id
        List<Long> bookingIdList = apartFeeMapper.selectBecomeDue();

        if(bookingIdList.size() == 0){
            return;
        }
        //根据订单id查出订单数据
        List<Booking> bookingList = bookFeign.getBookByIds(bookingIdList);

        //应该缴费时间为今日加一天
        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.DAY_OF_MONTH, 1);

        List<OverdueAlarm> overdueAlarmList = bookingList.stream().map(book -> {
            Integer customerId = book.getCustomerId();
            //获得客户手机,客户email
            Customer customerById = userFeign.getCustomerById(Long.valueOf(customerId));

            OverdueAlarm overdueAlarm = new OverdueAlarm();
            overdueAlarm.setRoomId(Long.valueOf(book.getRoomId()));
            overdueAlarm.setRoomNo(book.getRoomNo());
            overdueAlarm.setBedId(Long.valueOf(book.getBedId()));
            overdueAlarm.setBedNo(book.getBedNo());
            overdueAlarm.setBookingId(book.getId());
            overdueAlarm.setApartmentId(Long.valueOf(book.getApartmentId()));
            overdueAlarm.setCustomerId(Long.valueOf(book.getCustomerId()));
            overdueAlarm.setApartmentName(book.getApartmentName());
            overdueAlarm.setCustomerEmail(customerById.getCustomerEmail());
            overdueAlarm.setCustomerPhone(customerById.getCustomerPhone());
            overdueAlarm.setPlanPayTime(instance.getTime());
            overdueAlarm.setAlarmTime(new Date());
            overdueAlarm.setStatus(0);
            overdueAlarm.setDueStatus(0);
            return overdueAlarm;
        }).collect(Collectors.toList());

        //批量添加房租预警
        overdueAlarmMapper.batchInsert(overdueAlarmList);

    }
}
